package com.trkj.service.wxy;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wxy.ExamArrangeDao;
import com.trkj.dao.wxy.ExamDao;
import com.trkj.dao.wxy.QuestionBankDao;
import com.trkj.dao.wxy.StaffInformationDao;
import com.trkj.pojo.Admission;
import com.trkj.pojo.ExamArrange;
import com.trkj.pojo.ExamGrade;
import com.trkj.pojo.ExamPaper;
import com.trkj.pojo.QuestionBank;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.ExamPageVo;



@Service
@Transactional(rollbackFor = Exception.class)
public class ExamService {
	@Autowired
	private ExamDao examDao;
	@Autowired
	private ExamArrangeDao examArrangeDao;
	@Autowired
	private QuestionBankDao questionBankDao;
	
	@Autowired
	private StaffInformationDao staffInformationDao;
	/**
	 * 查询所有的记录分页
	 */
	public List<ExamPageVo> queryByExamArrangeQuestion(int page,int rows){
		List<Object[]> list = examDao.queryByExam(new PageRequest(page-1, rows));
		List<ExamPageVo> listExamPageVo = new ArrayList<>();
		if(list != null){
			for(Object[] objs: list){
				ExamPaper e = (ExamPaper) objs[0];
				QuestionBank q = (QuestionBank) objs[1];
				
				ExamPageVo examPageVo = new ExamPageVo();
				examPageVo.setEpId(e.getEpId());
				examPageVo.setClassSjName(e.getClassSjName());
				examPageVo.setEpStarttime(e.getEpStarttime());
				examPageVo.setEpEndtime(e.getEpEndtime());
				examPageVo.setEpShichang(e.getEpShichang());
				examPageVo.setEpDefen(e.getEpDefen());
				examPageVo.setEpState(e.getEpState());
				examPageVo.setEpDate(e.getEpDate());
				examPageVo.setEpWay(e.getEpWay());
				examPageVo.setEpGrade(e.getEpGrade());
				examPageVo.setEpTotal(e.getEpTotal());
				examPageVo.setEpNumberQuestion(e.getEpNumberQuestion());
				examPageVo.setEpTakeMeasure(e.getEpTakeMeasure());
				examPageVo.setEpHaveTest(e.getEpHaveTest());
				examPageVo.setEpNumberTotal(e.getEpNumberTotal());
				examPageVo.setQuName(q.getQuName());
				
				listExamPageVo.add(examPageVo);
			}
		}
		return listExamPageVo;
		
	}
	
	
	/**
	 * 我的考卷查询所有的记录分页
	 */
	public List<ExamPageVo> queryByExam(Long stainId){ 
		//拿到用户
		StaffInformation sf= staffInformationDao.findOne(stainId);
		//查看用户有多少准考证
		List<Admission> ams=sf.getAdmissions();
		List<ExamPageVo> listVo = new ArrayList<>();
				
		for (Admission am : ams) {
			
			List<ExamGrade> exg= am.getExamGrades();
			 for (ExamGrade examGrade : exg) {
				//查看用准考证有多少场考试
				  ExamArrange ex=examGrade.getExamArrange();
				  //查看场考试有多少试卷
					List<ExamPaper> ep =ex.getExamPapers();
					for (ExamPaper examPaper : ep) {
						ExamPageVo	vo=new ExamPageVo();
						vo.setEpId(examPaper.getEpId());
						vo.setArrangeName(ex.getArrangeName());
						vo.setQuName(examPaper.getQuestionBank().getQuName());
						vo.setClassSjName(examPaper.getClassSjName());
						vo.setEpStarttime(examPaper.getEpStarttime());
						vo.setEpEndtime(examPaper.getEpEndtime());
						vo.setEpShichang(examPaper.getEpShichang());
						vo.setEpDefen(examPaper.getEpDefen());
						vo.setEpState(examPaper.getEpState());
						listVo.add(vo);
					}
					
			}
			 
		}
		return listVo;
		/*List<Object[]> list = examDao.queryByExam(new PageRequest(page-1, rows));
		List<ExamPageVo> listExamPageVo = new ArrayList<>();
		if(list != null){
			for(Object[] objs: list){
				ExamPaper e = (ExamPaper) objs[0];
				ExamArrange r = (ExamArrange) objs[1];
				QuestionBank q = (QuestionBank) objs[2];
				
				ExamPageVo examPageVo = new ExamPageVo();
				examPageVo.setEpId(e.getEpId());
				examPageVo.setClassSjName(e.getClassSjName());
				examPageVo.setEpStarttime(e.getEpStarttime());
				examPageVo.setEpEndtime(e.getEpEndtime());
				examPageVo.setEpShichang(e.getEpShichang());
				examPageVo.setEpDefen(e.getEpDefen());
				examPageVo.setEpState(e.getEpState());
				examPageVo.setEpDate(e.getEpDate());
				examPageVo.setEpWay(e.getEpWay());
				examPageVo.setEpGrade(e.getEpGrade());
				examPageVo.setEpTotal(e.getEpTotal());
				examPageVo.setEpNumberQuestion(e.getEpNumberQuestion());
				examPageVo.setEpTakeMeasure(e.getEpTakeMeasure());
				examPageVo.setEpHaveTest(e.getEpHaveTest());
				examPageVo.setEpNumberTotal(e.getEpNumberTotal());
				examPageVo.setQuName(q.getQuName());
				examPageVo.setArrangeName(r.getArrangeName());
				
				listExamPageVo.add(examPageVo);
			}
		}
		return listExamPageVo;*/
		
	}
	
	
	/**
	 * 查询所有的记录题库
	 */
	public List<ExamPaper> findAll_examName(){
		return (List<ExamPaper>) examDao.findAll();
	}
	
	/**
	 * 分页查询 
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<ExamPaper> queryByPage(int page, int rows){
		return examDao.queryByPage(new PageRequest(page - 1, rows));
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return examDao.count();
		
	}
	
	public  ExamPaper  fallonid(long id){
		return examDao.findOne(id);
	}
	/**
	 * 根据考试的需求进行查询 
	 * @param eId
	 * @param eIdstartDate
	 * @param eIdendTime
	 * @return
	 */
	public List<ExamPageVo> queryByName(String classSjName){
		return examDao.queryByeName("%"+classSjName+"%");
	}
	
	/**
	 * 新增
	 * @param exPaper
	 * @param exArrange
	 * @param quBank
	 * @throws ServiceException
	 */
	public void addExamPaper(ExamPaper exPaper,QuestionBank quBank ) throws ServiceException{
		try {
			
			QuestionBank qu=questionBankDao.findOne(quBank.getQuId());
			exPaper.setEpNumberQuestion(new BigDecimal(0));
			exPaper.setEpNumberTotal(new BigDecimal(0));
			exPaper.setEpHaveTest(new BigDecimal(0));
			exPaper.setEpTakeMeasure(new BigDecimal(0));
			exPaper.setEpDate(new Timestamp(new Date().getTime()));
			
			exPaper.setQuestionBank(qu);
			
			examDao.save(exPaper);
		
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param upId
	 * @throws ServiceException
	 */
	public void kssj_delete(Long epId) throws ServiceException{
		try {
			examDao.delete(epId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	/**
	 * 删除
	 * @param upId
	 * @throws ServiceException
	 */
	public void ksap_delete(Long epId) throws ServiceException{
		try {
			examDao.delete(epId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	/**
	 * 考试试卷编辑
	 * @param epId
	 * @param examPageVo
	 * @return
	 * @throws ServiceException
	 */
	public ExamPageVo updateExamArrangeQuestion(Long epId,ExamPageVo examPageVo) throws ServiceException{
		
		try {
			ExamPaper examPaper = examDao.findOne(epId);
			QuestionBank questionBank = questionBankDao.findOne(examPaper.getQuestionBank().getQuId());
			examPageVo.setEpId(epId);
			examPageVo.setClassSjName(examPaper.getClassSjName());
			examPageVo.setEpTotal(examPaper.getEpTotal());
			examPageVo.setEpShichang(examPaper.getEpShichang());
			examPageVo.setQuId(questionBank.getQuId());
			examPageVo.setQuName(questionBank.getQuName());
			examPageVo.setEpGrade(examPaper.getEpGrade());
			examPageVo.setEpWay(examPaper.getEpWay());
			examPageVo.setEpState(examPaper.getEpState());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		return examPageVo;
		
	}
	
	/**
	 * 考试试卷编辑之后保存
	 * @param epId
	 * @param examPageVo
	 * @throws ServiceException
	 */
	public void updateSavaExamArrangeQuestion(Long epId,ExamPageVo examPageVo) throws ServiceException{
		try {
			ExamPaper examPaper = examDao.findOne(epId);
			QuestionBank questionBank = questionBankDao.findOne(examPageVo.getQuId());
			
			examPaper.setEpId(epId);
			examPaper.setClassSjName(examPageVo.getClassSjName());
			examPaper.setEpTotal(examPageVo.getEpTotal());
			examPaper.setEpShichang(examPageVo.getEpShichang());
			examPaper.setQuestionBank(questionBank);
			examPaper.setEpGrade(examPageVo.getEpGrade());
			examPaper.setEpWay(examPageVo.getEpWay());
			examPaper.setEpState(examPageVo.getEpState());
			
			examDao.save(examPaper);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
}
